from PyQt5.QtWidgets import QDialog, QApplication, QDialogButtonBox

from addContact_ui import Ui_Dialog


class Form(QDialog, Ui_Dialog):
    StyleSheet = """
           QComBox{color:darkblue}
           QLineEdit{color:darkgreen}
           QLineEdit[mandatory="1"]{
           background-color:rgb(254,254,127);
           color:darkblue;
           }
           """

    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.setupUi(self)

        self.lineedits = (self.forenameEdit, self.surnameEdit, self.companyEdit, self.phoneEdit, self.emailEdit)
        for lineEdit in self.lineedits:
            lineEdit.setProperty("mandatory", 1)
            lineEdit.textEdited.connect(self.updateUi)

        self.setStyleSheet(Form.StyleSheet)
        self.categoryComboBox.activated.connect(self.updateUi)
    def updateUi(self):
        mandatory = bool(int(self.companyEdit.property("mandatory")))
        if self.categoryComboBox.currentText() == "Bussness":
            if not mandatory:
                self.companyEdit.setProperty("mandatory", 1)
        elif mandatory:
            self.companyEdit.setProperty("mandatory", 0)
        if (mandatory !=
                bool(int(self.companyEdit.property("mandatory")))):
            self.setStyleSheet(Form.StyleSheet)
        enable = True
        for lineEdit in self.lineedits:
            if (bool(int(lineEdit.property("mandatory"))) and
                    not lineEdit.text()):
                enable = False
                break
        self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(enable)


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    form = Form()
    form.show()
    sys.exit(app.exec())
